package com.manage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.controller.request.RepairRequest;
import com.manage.controller.response.RepairTaskListResponse;
import com.manage.controller.request.RepairTaskListRequest;
import com.manage.pojo.entity.RepairMaterial;
import com.manage.pojo.entity.RepairMaterialLog;
import com.manage.pojo.entity.RepairTask;
import com.manage.pojo.entity.User;
import com.manage.service.RepairMaterialLogService;
import com.manage.service.RepairMaterialService;
import com.manage.service.RepairTaskService;
import com.manage.mapper.RepairTaskMapper;
import com.manage.service.UserService;
import com.manage.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author chenximeng
* @description 针对表【repair_task】的数据库操作Service实现
* @createDate 2022-01-03 16:46:09
*/
@Service
public class RepairTaskServiceImpl extends ServiceImpl<RepairTaskMapper, RepairTask>
    implements RepairTaskService{

    @Autowired
    RepairTaskMapper repairTaskMapper;

    @Autowired
    RepairMaterialService repairMaterialService;

    @Autowired
    RepairMaterialLogService repairMaterialLogService;

    @Autowired
    RepairTaskService repairTaskService;

    @Autowired
    UserService userService;

    @Override
    public List<RepairTaskListResponse> listTask(RepairTaskListRequest request) {
        List<RepairTask> list = repairTaskMapper.listTask(request);
        List<RepairTaskListResponse> result = ObjectUtil.cloneList(list, RepairTaskListResponse.class);
        result.forEach(item -> {
            Long checkUserId = item.getCheckUser();
            Long repairUserId = item.getRepairUser();
            User checkUser = userService.getById(checkUserId);
            if (checkUser != null)  item.setCheckUserName(checkUser.getUsername());
            User repairUser = userService.getById(repairUserId);
            if (repairUser != null) item.setRepairUserName(repairUser.getUsername());
        });
        return result;
    }

    @Override
    public List<RepairTaskListResponse> listNeedRepair(RepairTaskListRequest request) {
        List<RepairTask> list = repairTaskMapper.listNeedRepair(request);
        List<RepairTaskListResponse> result = ObjectUtil.cloneList(list, RepairTaskListResponse.class);
        result.forEach(item -> {
            Long checkUserId = item.getCheckUser();
            Long repairUserId = item.getRepairUser();
            User checkUser = userService.getById(checkUserId);
            if (checkUser != null)  item.setCheckUserName(checkUser.getUsername());
            User repairUser = userService.getById(repairUserId);
            if (repairUser != null) item.setRepairUserName(repairUser.getUsername());
        });
        return result;
    }

    @Override
    public List<RepairTaskListResponse> listNeedCheck(RepairTaskListRequest request) {
        List<RepairTask> list = repairTaskMapper.listNeedCheck(request);
        List<RepairTaskListResponse> result = ObjectUtil.cloneList(list, RepairTaskListResponse.class);
        result.forEach(item -> {
            Long checkUserId = item.getCheckUser();
            Long repairUserId = item.getRepairUser();
            User checkUser = userService.getById(checkUserId);
            if (checkUser != null)  item.setCheckUserName(checkUser.getUsername());
            User repairUser = userService.getById(repairUserId);
            if (repairUser != null) item.setRepairUserName(repairUser.getUsername());
        });
        return result;
    }

    @Override
    @Transactional
    public void repair(RepairRequest request) {
        RepairMaterialLog repairMaterialLog = new RepairMaterialLog();
        repairMaterialLog.setMaterialId(request.getMaterialId());
        repairMaterialLog.setTaskId(request.getTaskId());
        repairMaterialLog.setNum(request.getNum());
        repairMaterialLog.setUserId(repairTaskService.getById(request.getTaskId()).getRepairUser());
        repairMaterialLogService.save(repairMaterialLog);
        repairMaterialService.decrese(request.getMaterialId(), request.getNum());
    }

    @Override
    public List<RepairTaskListResponse> listMoney(RepairTaskListRequest request) {
        List<RepairTask> list = repairTaskMapper.listMoney(request);
        List<RepairTaskListResponse> result = ObjectUtil.cloneList(list, RepairTaskListResponse.class);
        result.forEach(item -> {
            Long checkUserId = item.getCheckUser();
            Long repairUserId = item.getRepairUser();
            User checkUser = userService.getById(checkUserId);
            if (checkUser != null)  item.setCheckUserName(checkUser.getUsername());
            User repairUser = userService.getById(repairUserId);
            if (repairUser != null) item.setRepairUserName(repairUser.getUsername());
        });
        return result;
    }
}




